package com.junzerg.tooltip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.junzerg.tooltip.entity.dao.ConvAmapAddressDao;
import com.junzerg.tooltip.entity.dao.ConvDao;
import com.junzerg.tooltip.service.IAmapService;
import com.junzerg.tooltip.service.IConvAmapAddressService;
import com.junzerg.tooltip.service.IConvService;
import com.junzerg.tooltip.service.IPoiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author pengjz <br>
 * @version 1.0 <br>
 * @description PoiServiceImpl <br>
 * @date 2022/4/11 12:16 <br>
 */
@Service
@Slf4j
public class PoiServiceImpl implements IPoiService {

    @Resource
    IConvService convService;

    @Resource
    IAmapService amapService;

    @Resource
    IConvAmapAddressService convAmapAddressService;

    static Map<String, String> cityCode = new HashMap<>();

    static {
        cityCode.put("上海市", "310000");
        cityCode.put("上海市市辖区", "310100");
        cityCode.put("黄浦区", "310101");
        cityCode.put("徐汇区", "310104");
        cityCode.put("长宁区", "310105");
        cityCode.put("静安区", "310106");
        cityCode.put("普陀区", "310107");
        cityCode.put("虹口区", "310109");
        cityCode.put("杨浦区", "310110");
        cityCode.put("闵行区", "310112");
        cityCode.put("宝山区", "310113");
        cityCode.put("嘉定区", "310114");
        cityCode.put("浦东新区", "310115");
        cityCode.put("金山区", "310116");
        cityCode.put("松江区", "310117");
        cityCode.put("青浦区", "310118");
        cityCode.put("奉贤区", "310120");
        cityCode.put("崇明区", "310151");
    }

    @Override
    public void amapCralwer() {
        // 所有已经爬取的地址
        Set<String> set = convAmapAddressService.list(
        ).stream().map(ConvAmapAddressDao::getConvAddress).collect(Collectors.toSet());
        //  还没有爬取的地址
        Map<String, List<ConvDao>> collect = convService.list(
//                new QueryWrapper<ConvDao>().eq("address", "虹桥路1041弄")
        ).stream().filter(dao -> !set.contains(dao.getAddress())).collect(
                Collectors.groupingBy(
                        ConvDao::getAddress
                )
        );
        AtomicInteger index = new AtomicInteger();
        collect.forEach(
                ((s, convDaos) -> {
                    ConvDao convDao = convDaos.get(0);
                    amapService.getPlaceByTextFromAmap(
                            s,
                            "120000",
                            cityCode.get(convDao.getDistinct()),
                            true,
                            20, 1
                    );
                    index.getAndIncrement();
                    if (index.get() % 10 == 0) {
                        try {
                            log.info("完成了{}", index.get());
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                })
        );
    }
}
